#include "StaticMeshResource.h"
#include "Runtime/Logic/Resource/StaticMesh.h"
#include "Runtime/Thread/RenderingThreadContext.h"

namespace Alice
{
    void StaticMeshVertexFactories::InitResources(const MeshVertexBuffers& VertexBuffers, uint32 LODIndex, const StaticMesh* inOwner)
    {
        InitVertexFactory(VertexBuffers,mVertexFactory,0,inOwner,false);
        //BeginInitResource(&mVertexFactory);
    }
    void StaticMeshVertexFactories::InitVertexFactory(const MeshVertexBuffers& VertexBuffers, LocalVertexFactory& InOutVertexFactory, uint32 LODIndex, const StaticMesh* inOwnerMesh, bool bInOverrideColorVertexBuffer)
    {
        struct InitStaticMeshVertexFactoryParams
        {
            LocalVertexFactory* VertexFactory;
            const MeshVertexBuffers* VertexBuffers;
            uint32 LightMapCoordinateIndex;
            uint32 LODIndex;
            uint8 bOverrideColorVertexBuffer : 1;
        } Params;
        Params.VertexFactory				= &InOutVertexFactory;
        Params.VertexBuffers				= &VertexBuffers;
        Params.bOverrideColorVertexBuffer	= bInOverrideColorVertexBuffer;
        Params.LightMapCoordinateIndex		= 0;
        Params.LODIndex						= LODIndex;
        ENQUEUE_RENDER_COMMAND(InitStaticMeshVertexFactory)(
        [Params](CGICommandListImmediate& inCGICmdList)
        {
            LocalVertexFactory::FDataType data;
            Params.VertexBuffers->mPositionBuffer->BindPositionVertexBuffer(Params.VertexFactory,data);
            Params.VertexFactory->SetData(inCGICmdList,data);
            Params.VertexFactory->InitResource(inCGICmdList);
        });
    }

    void StaticMeshLODResource::InitResources(StaticMesh* inParent, int32 LODIndex)
    {
        BeginInitResource(mMeshVertexBuffers->mPositionBuffer);
    }
    void StaticMeshResource::InitResources(StaticMesh* inOwner)
    {
        mStaticMeshLODResource[0].InitResources(inOwner,0);
        mStaticMeshLODVertexFactories[0].InitResources(*mStaticMeshLODResource[0].mMeshVertexBuffers,0,inOwner);
    }
}
